var dom = document.getElementById("map");
var pie_dom = document.getElementById("pie");
var bar_dom = document.getElementById("bar");
var myChart = echarts.init(dom,'dark');

var pie = echarts.init(pie_dom,'dark');
var bar = echarts.init(bar_dom,'dark');

var option_pie = { 
  title : {
    text: '访客浏览器分布',
    x: 'center',
    textStyle: {
      fontSize: 14
    }
  },
  tooltip : {
    trigger: 'item',
    formatter: "{a} <br/>{b} : {c} ({d}%)"
  },
  legend: {
    orient : 'vertical',
    x : 'left',
    data:['Chrome','Firefox','IE 8.0','Safari','其它浏览器']
  },
  series : [{
    name:'访问来源',
    type:'pie',
    radius : '55%',
    center: ['50%', '50%'],
    data:[
      {value:9052, name:'Chrome'},
      {value:1610, name:'Firefox'},
      {value:3200, name:'IE 8.0'},
      {value:535, name:'Safari'},
      {value:1700, name:'其它浏览器'}
    ]
  }]
}

var option_bar = {
    title : {
        text: '访客浏览器分布',
        x: 'center',
        textStyle: {
          fontSize: 14
        }
    },
    color: ['#3398DB'],
    tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    xAxis : [
        {
            type : 'category',
            data : ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
                alignWithLabel: true
            }
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [{
            name:'直接访问',
            type:'bar',
            barWidth: '60%',
            data:[10, 52, 200, 334, 390, 330, 220]
        }
    ]
};


$.get('data/hk.json', function (geoJson) {

    myChart.hideLoading();

    echarts.registerMap('HK', geoJson);

    myChart.setOption(option = {
        title: {
            text: '香港18区人口密度 （2011）',
            subtext: '人口密度数据来自Wikipedia',
            sublink: 'http://www.baidu.com'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}<br/>{c} (p / km2)'
        },
        toolbox: {
            show: true,
            orient: 'vertical',
            left: 'right',
            top: 'center',
            feature: {
                dataView: {readOnly: false},
                restore: {},
                saveAsImage: {}
            }
        },
        visualMap: {
            min: 800,
            max: 50000,
            text:['High','Low'],
            realtime: false,
            calculable: true,
            textStyle:{
                color:'#fff'
            },  
            inRange: {
                color: ['lightskyblue','yellow', 'orangered']
            }
        },
        series: [
            {
                name: '香港18区人口密度',
                type: 'map',
                mapType: 'HK', // 自定义扩展图表类型
                itemStyle:{
                    normal:{label:{
                        show:true,
                        color:'#fff',
                        textBorderWidth:1,
                        textBorderColor :'#000000',
                        fontWeight:'bold',
                        fontSize:'14'
                    }},
                    emphasis:{label:{
                        show:true,
                        color:'#f00'
                    }}
                },
                data:[
                    {name: '中西区', value: 20057.34},
                    {name: '湾仔', value: 15477.48},
                    {name: '东区', value: 31686.1},
                    {name: '南区', value: 6992.6},
                    {name: '油尖旺', value: 44045.49},
                    {name: '深水埗', value: 40689.64},
                    {name: '九龙城', value: 37659.78},
                    {name: '黄大仙', value: 45180.97},
                    {name: '观塘', value: 55204.26},
                    {name: '葵青', value: 21900.9},
                    {name: '荃湾', value: 4918.26},
                    {name: '屯门', value: 5881.84},
                    {name: '元朗', value: 4178.01},
                    {name: '北区', value: 2227.92},
                    {name: '大埔', value: 2180.98},
                    {name: '沙田', value: 9172.94},
                    {name: '西贡', value: 3368},
                    {name: '离岛', value: 806.98}
                ],
                // 自定义名称映射
                nameMap: {
                    'Central and Western': '中西区',
                    'Eastern': '东区',
                    'Islands': '离岛',
                    'Kowloon City': '九龙城',
                    'Kwai Tsing': '葵青',
                    'Kwun Tong': '观塘',
                    'North': '北区',
                    'Sai Kung': '西贡',
                    'Sha Tin': '沙田',
                    'Sham Shui Po': '深水埗',
                    'Southern': '南区',
                    'Tai Po': '大埔',
                    'Tsuen Wan': '荃湾',
                    'Tuen Mun': '屯门',
                    'Wan Chai': '湾仔',
                    'Wong Tai Sin': '黄大仙',
                    'Yau Tsim Mong': '油尖旺',
                    'Yuen Long': '元朗'
                }
            }
        ]
    });
});;

pie.setOption(option_pie, true);
bar.setOption(option_bar, true);

/*窗口自适应，关键代码*/
setTimeout(function (){
    window.onresize = function () {
        pie.resize();
        myChart.resize();
        bar.resize();
    }
},200)